package fast.data;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class StatBlock implements Serializable {

	private static final long serialVersionUID = -7933054359479646860L;
	public static final StatBlock ZERO_BLOCK = new StatBlock();
    private final double[] data;
    
    public StatBlock() {
        data = new double[Stat.possibleStats.length];
    }

    public StatBlock(int str, int agi, int sta, int intt, int spi, int hit, int mst, int hst, int exp, int cri, int ap, int sp) {
        data = new double[Stat.possibleStats.length];
        data[Stat.str.getIndex()] = str;
        data[Stat.agi.getIndex()] = agi;
        data[Stat.sta.getIndex()] = sta;
        data[Stat.intt.getIndex()] = intt;
        data[Stat.spi.getIndex()] = spi;
        data[Stat.hit.getIndex()] = hit;
        data[Stat.mst.getIndex()] = mst;
        data[Stat.hst.getIndex()] = hst;
        data[Stat.exp.getIndex()] = exp;
        data[Stat.cri.getIndex()] = cri;
        data[Stat.ap.getIndex()] = ap;
        data[Stat.sp.getIndex()] = sp;
    }
    
    public StatBlock(JSONObject wowheadData) {
    	data = new double[Stat.possibleStats.length];
    	try{
	        for (Stat stat : Stat.possibleStats) {
	            if (wowheadData.has(stat.getWowheadName())) {
	                data[stat.getIndex()] = wowheadData.getInt(stat.getWowheadName());
	            } else {
	                data[stat.getIndex()] = 0;
	            }
	        }
    	}catch(JSONException e){
    		e.printStackTrace();
    	}
    }
    
    /*private StatBlock(int data[]) {
        this.data = data;
    }*/
    
    public StatBlock(JSONArray array) {
    	data = new double[Stat.possibleStats.length];
    	try{
			for(int i = 0; i < array.length(); i++){
				JSONObject line = array.getJSONObject(i);
				int statid = line.getInt("stat");
				int amount = line.getInt("amount");
				for (Stat stat : Stat.possibleStats) {
					if(stat.getId() == statid){
						data[stat.getIndex()] = amount;
					}
				}
			}
    	}catch(JSONException e){
    		e.printStackTrace();
    	}
	}

	public void add(StatBlock other) {
        int newData[] = new int[Stat.possibleStats.length];
        for (int i = 0; i < newData.length; ++i) {
            data[i] += other.data[i];
        }
    }

    public void subtract(StatBlock other) {
        int newData[] = new int[Stat.possibleStats.length];
        for (int i = 0; i < newData.length; ++i) {
            data[i] -= other.data[i];
        }
    }
    
    public double getStat(Stat stat) {
        return data[stat.getIndex()];
    }
    
    public void setStat(Stat stat, double d) {
        data[stat.getIndex()] = d;
    }
    /**
     * Increases the value of the Stat by value
     * @param stat
     * @param value
     */
    public void addStat(Stat stat, int value) {
        data[stat.getIndex()] += value;
    }
    
    public boolean equalStats(StatBlock sb){
    	if(sb == null) return false;
    	for(Stat stat : Stat.values()){
    		if(this.getStat(stat) != sb.getStat(stat))
    			return false;
    	}
    	return true;
    }
    
    public String toString(){
    	StringBuilder builder = new StringBuilder();
        boolean first = true;
        builder.append("[");
        for (Stat stat : Stat.possibleStats) {
        	if(data[stat.getIndex()] == 0){ continue; }
            if (!first) {
                builder.append(" ");
            }
            first = false;
   
            builder.append(stat.getName());
            builder.append("=");
            builder.append(data[stat.getIndex()]);
        }
        builder.append("]");

        return builder.toString();
    }
}
